import java.util.*;

/* *********************************************************** *\
	ICSFormat Class
	-A static class. Provides data checks and reformats stuff. 

	Change Log
	-7/30 wrote valid_syntax(); unsure about what to do with errors. currently just prints a stored msg. 

	Functions
	-valid_syntax(type,key,value)	|	checks syntax only to see if valid. returns error msg.
	-valid_value(type,key,value)	|	checks value. does type conversions and returns proper format. 
	
\* *********************************************************** */
public class ICSFormat{
	private ICSFormat(){
	}
	//Intended for realtime syntax only checks. 
	//formatting and context checks can happen on submit instead.
	public static String valid_syntax(String type, String key, String value){
		//"" is success. otherwise, returns proper errorMsg
		String[] errorMsg;
		String error="Failed to check cases.";
		switch(Arrays.asList("date","time","text").indexOf(type)){
			case 0: //date type
				errorMsg=new String[]{"Invalid date",""};
				error=errorMsg[(valid_date(key, value))];
				break;
			case 1: //time type
				errorMsg=new String[]{"Invalid time",""};
				error=errorMsg[(valid_time(key, value))];
				break;
			case 2: //text type
				errorMsg=new String[]{"Invalid characters. [.,/?!] are allowed",""};
				error=errorMsg[(valid_text(key, value))];
				break;
			default://bad input
				error="Failed to match type";
				break;
		}
		return error;

	}
		private static int valid_date(String key, String value){
			//valid syntaxes:  MM/DD/YYYY, M/D/YY, M-DD-YYYY, MM.D.YYYY, MM DD YY
			value=value.replaceAll("(\\-|\\.|\\s)","/");
			if(value.matches("(0[1-9]|1[0-2]|[1-9])/([0-2][0-9]|3[0-1]|[0-9])/([0-9][0-9]|[1-2][0-9][0-9][0-9])")) return 1;
			return 0;
		}
		private static int valid_time(String key, String value){
			//valid syntaxes: 12:01am, 24:00, 15:01:40, 1:29 pm
			if(value.matches("((?:(?:[0-1][0-9])|(?:[2][0-3])|(?:[0-9])):(?:[0-5][0-9])(?::[0-5][0-9])?(?:\\s?(?:am|AM|pm|PM))?)")) 				return 1; 
			return 0;
		}
		private static int valid_text(String key, String value){
			//whitelist valid characters a-z, 0-9, ._/?!, space	
			if(value.matches("^[0-9a-zA-Z\\.,?!_/\\s]{1,}$")) return 1;
			return 0;
		}

/*	//attempts to convert inputs to object type to test for valid values
	public static String valid_value(String type, String key, String value){
		//"" is success. otherwise, returns proper errorMsg
		switch(Arrays.asList("date","time","text").indexOf(type)){
			case 0: //date type
				break;
			case 1: //time type
				break;
			case 2: //text type
				break;
			default://bad input
				break;
		}
		return error;

	}
*/
}
